﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [AutomaticTemplate]
    public class SubSystemController : Controller
    {
        // GET: SubSystem
        public ActionResult Index(string containerId)
        {
            return View(new SubSystemIndexModel());
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var pageInfo = proxy.Channel.GetSubSystemPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }

        public ActionResult GetSubSystems(Guid ids)
        {
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var pageInfo = proxy.Channel.GetSubSystemInfo(ids);

                return this.JsonNet(pageInfo);
            }
        }
         
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SubSystemDTO dto = null;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                dto = proxy.Channel.GetSubSystemInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return View(dto.To<SubSystemModel>());
        }
         
        public ActionResult Create()
        {
            return View(new SubSystemModel());
        }

         
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            SubSystemDTO dto = null;
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                dto = proxy.Channel.GetSubSystemInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }


            return View(dto.To<SubSystemModel>());
        }


        [HttpPost]
        public ActionResult Save(SubSystemModel vm)
        {
            SubSystemDTO dto = vm.To<SubSystemDTO>();

            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.SaveSubSystem(dto);

                return this.JsonNet(res);
            }
        }

        [HttpPost]
        public ActionResult Delete(Guid[] ids)
        {
            var res = new Result();
            using (var proxy = new ServiceProxy<IPlatformService>())
            {
                res.Data = proxy.Channel.DeleteSubSystem(ids);
            }

            return this.JsonNet(res);
        }
    }
}